Java String

    Java String

    In Java, you can represent the string as an array of the char values. For example-

    Char[] c= {‘h’,’e’,’l’,’l’,’o’};
    
    String s= new string(c);

    Or

    String s=”hello”;

    There are a number of methods available under the string class of java that allows you to perform various actions on the strings. Java allows the string class to implement serializable, comparable and charSequence interfaces.

    What is a CharSequence interface

    This interface in java will allow you to represent the sequence of characters. This interface will then be implemented by the string, StringBuffer and the StringBuilder class. These three classes will allow you to create strings in Java.

    Java strings are immutable which means you cannot change the string. If you want to change the string, you need to create a new instance. If you are using the mutable string then you can use StringBuffer and StringBuilder class.

    What is a string in Java

    The string is considered to be the sequence of the characters. Java provides you java.lang.String class to create the string object. You can create a string object using two methods- using a string literal and by using the new keyword.

    Using a string literal-

    String str= “hello”;

    Whenever you create a string literal the JVM searches the string constant pool. If there is no string exists then the JVM will create the instance for the string and place the instance in the string pool. But if the string exists already then it will return the instance from the pool.

    String str1=” hello”;
    
    String str2=” hello”; 

    The second statement will not create the instance for str2 as the str1 is already existed having the same value.

    Java uses the concept of the string literal to make Java a memory-efficient language.

    By the new keyword-

    String str= new string(“hello”);

    This statement will create two objects and one reference variable.

    This new string object will be created by the JVM in the heap memory. The literal “hello” will be saved to the string constant pool. The string variable str will then refer to the object created in the heap.

    Methods in the java string class

    Below is the list of the methods that can be used in reference to the strings.

    • char charAt(int index)
    • int length()
    • static String format(String format, Object... args)
    • static String format(Locale l, String format, Object... args)
    • String substring(int beginIndex)
    • String substring(int beginIndex, int endIndex)
    • boolean contains(CharSequence s)
    • static String join(CharSequence delimiter, CharSequence... elements)
    • static String join(CharSequence delimiter, Iterable<? extends CharSequence> elements)
    • boolean equals(Object another)
    • boolean isEmpty()
    • String concat(String str)
    • String replace(char old, char new)
    • String replace(CharSequence old, CharSequence new)
    • static String equalsIgnoreCase(String another)
    • String[] split(String regex)
    • String[] split(String regex, int limit)
    • String intern()
    • int indexOf(int ch)
    • int indexOf(int ch, int fromIndex)
    • int indexOf(String substring)
    • int indexOf(String substring, int fromIndex)
    • String toLowerCase()
    • String toLowerCase(Locale l)
    • String toUpperCase()
    • String toUpperCase(Locale l)
    • String trim()
    • static String valueOf(int value)